package db

import (
	"errors"
	"hr/api/dto"

	"gorm.io/gorm"
)

type PublicityManage struct {
	AppModel
	UserID      string `gorm:"size:64" json:"user_id"`
	Title       string `json:"title"`
	Content     string `json:"content"`
	ContentText string `json:"content_text"`
	CheckTime   string `gorm:"size:64" json:"check_time"`
	CreateBy    string `gorm:"size:128" json:"createBy"` // 创建人
	UpdateBy    string `gorm:"size:128" json:"updateBy"` // 更新人
	Remark      string `gorm:"size:255" json:"remark"`   //备注
	Status      string `gorm:"size:64;" json:"status"`
	User        `gorm:"-" json:"user"`
	File        string `gorm:"size:255" json:"file"`
}

func (m *PublicityManage) TableName() string {
	return "publicity_manage"
}

type PublicityManagePageModel struct {
	Total    int64             `json:"total"`
	Result   []PublicityManage `json:"result"`
	PageNo   int               `json:"pageNo"`
	PageSize int               `json:"pageSize"`
}

func GetPublicityManagePaging(pagination dto.Pagination) (PublicityManagePageModel, error) {
	result := PublicityManagePageModel{
		Total:    0,
		Result:   make([]PublicityManage, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Model(&PublicityManage{}).Order("created_at desc")

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}

	if len(result.Result) > 0 {
		for i, manage := range result.Result {
			u, _, tag := GetUsers(map[string]interface{}{
				"id": manage.UserID,
			})
			if tag == "2" {
				result.Result[i].User = u
			}
		}
	}

	return result, nil
}

// GetPublicityManage 根据条件查找
func GetPublicityManage(p map[string]interface{}) (PublicityManage, error, string) {
	f := PublicityManage{}
	err := GetInstance().Where(p).First(&f).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return f, errors.New("查无记录"), "0"
		}
		return f, errors.New(err.Error()), "1"
	}
	return f, nil, "2"
}

func CreatePublicityManage(p dto.PublicityManageRequest, user string) (PublicityManage, error) {
	d := PublicityManage{
		UserID:      p.UserId,
		Title:       p.Title,
		Content:     p.Content,
		ContentText: p.ContentText,
		CheckTime:   p.CheckTime,
		CreateBy:    user,
		File:        p.File,
	}
	if err := GetInstance().Create(&d).Error; err != nil {
		return d, err
	}
	return d, nil
}

func UpdatePublicityManage(id uint, p dto.PublicityManageRequest, userId string) (PublicityManage, error) {
	f := PublicityManage{
		AppModel: AppModel{
			ID: id,
		},
	}
	err := GetInstance().Model(&f).Updates(PublicityManage{
		UserID:      p.UserId,
		Title:       p.Title,
		Content:     p.Content,
		ContentText: p.ContentText,
		CheckTime:   p.CheckTime,
		UpdateBy:    userId,
		Status:      p.Status,
		File:        p.File,
	}).Updates(map[string]interface{}{
		"id": id,
	}).Scan(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}

func DeletePublicityManage(id uint) (PublicityManage, error) {
	f := PublicityManage{}
	err := GetInstance().Unscoped().Where("id = ?", id).Delete(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}
